<template>
  <div>
    <el-tree
      ref="tree"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
      :data="list"
      :props="{label:'name'}"
    />
    <el-button type="primary" @click="hSubmit">确定</el-button>
    <el-button>删除</el-button>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeList } from '@/utils'
import { getRoleDetail, assignPrem } from '@/api/settings'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    // 给角色分配权限
    async hSubmit() {
      try {
        // 获取树节点选中的节点 -> 角色拥有的权限
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPrem({ id: this.roleId, permIds }) // { id: this.roleId, permIds: permIds }
        console.log('assignPrem', res)
        // 角色权限分配成功
        this.$emit('success')
        // 保存数据
      } catch (err) {
        console.log('assignPrem', err)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        // console.log('getPermissionList', res)
        this.list = tranListToTreeList(res.data.data)
        const perIds = await getRoleDetail(this.roleId)
        console.log(perIds, '角色详情')
        this.$refs.tree.setCheckedKeys(perIds.data.data.permIds)
        // this.perIds = perIds.data.data.permIds
        // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
